Skip to main content

请求接口后 解析Json

请求接口后 解析Json

问题: unity自带的json解析函数 JsonUtility.FromJson<Type>(param) 解析不了字典类型的

解决方法

使用 JsonConvert.DeserializeObject<Type>(param2)

步骤:

  1. 下载 JsonConvert JsonNet.9.0.1.unitypackage

地址是: https://github.com/SaladLab/Json.Net.Unity3D/releases

  1. 导入unity里面后 引用
using Newtonsoft.Json;
  1. 使用
  • 定义结构
[System.Serializable]
public class GetMapBuildingDataBase
{
public int code;
public string msg;
public GetMapBuildingData data;
}

[System.Serializable]
public class GetMapBuildingData
{
public Dictionary<string, string> building_id_list ;
}
  • 调用
    public void RefreshUpgradeLayer(int map_id)
{

Debug.Log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
GetMapBuilding mapBuilding = new GetMapBuilding();
mapBuilding.token = PlayerPrefsManager.GetString("player_token");
mapBuilding.map_id = map_id;
//获取地图上的已升级的建筑
GameController.instance.HttpPostAsyncRequest<GetMapBuilding>(GameConfig.GET_MAP_BUILDING, mapBuilding, (param2) =>
{

GameDatabase.instance.getMapBuildingDataBase = JsonConvert.DeserializeObject<GetMapBuildingDataBase>(param2);
Dictionary<string, string> building_id_list = GameDatabase.instance.getMapBuildingDataBase.data.building_id_list;

if (GameDatabase.instance.levelStarDataBase.code == 0)
{
foreach (var item in building_id_list)
{
Debug.Log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + item.Key + "~~~~" + item.Value);
}
}
});
}